<?php
namespace Admin\Controller;
class ColumnController extends BaseController {
    public function index(){
    	$column = M('column');
        $count  =$column->count();
    	$list = $column->select();
        $this->assign('count',$count);
    	$this->assign('list',$list);
        $this->display();
    }
    public function add(){
    	$column=M('column');
    	$list =$column->select();
    	if(IS_GET){
    	$this->assign('list',$list);
    	$this->display();
    	}else{
    		$column =M('column');
    		//当pid==0的时候添加一级栏目
    		if($_POST['pid']==0){
    			$_POST['path']='0,';
    			$result=$column->add($_POST);
    			if($result){
    				$this->success('添加成功');
    			}else{
    				$this->error('添加失败');
    			}
    		}else{
    			//否则添加二级或三级栏目
    			$data=$column->where('id = '.$_POST['pid'])->find();
    			$_POST['path']=$data['path'].$data['id'].',';
    			$result=$column->add($_POST);
    			if($result){
    				$this->success('添加成功');
    			}else{
    				$this->error('添加失败');
    			}
    		}
   		}
    	
    
    }
    public function del(){
        $id =I('id');
        $column = M('Column');
        $data = $column->field('id')->where("path like '%0,{$id},%'")->select();

        $data['_logic']='or';
        $result = $column->where($data)->delete();
        $result = $column->where('id='.$id)->delete();
        //dump($result);
    	if($result){
    		$this->ajaxReturn(1);
    	}else{
    		$this->ajaxreturn(0);
    	}	
    }
    public function edit(){
        $id=I('id');
        $arr=M('column')->where('id='.$id)->find();
        $this->assign('arr',$arr);
    	$this->display();
    }
    public function editdo(){
        $res=M('column')->save($_POST);
        if($res){
            $this->success('修改成功');

        }else{
            $this->error('修改失败');
        }
    }
     public function add_photo(){
        $upload = new \Think\Upload();                                          // 实例化上传类
        $upload->maxSize = 1024*1024*10 ;                                       // 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');                     // 设置附件上传类型
        $upload->rootPath = 'Public/';                                       // 设置附件上传根目录
        $upload->savePath = 'admin/Uploads/images/';
        $info = $upload->uploadOne($_FILES['img']);
        if($info){
            $res['messsge']=$upload->getError();
            $res['result_code'] = 100;
            $res['file'] = '/public/'.$info['savepath'].$info['savename'];
        }else{
             $res['messsge']=$upload->getError();
            $res['result_code'] = 101;
        }
        $this->ajaxReturn($res);
    }

    
}